﻿using NUnit.Framework;
using ScrappyDB.Linq;
using System.Linq;

namespace ScrappyDB.IntegrationTests.AwsQuery101Examples
{
    /************************************************************************************
    *   Query 101: Building Amazon SimpleDB Queries
    *   Simple Query Examples     
    *   Source document: http://aws.amazon.com/articles/1231
    *************************************************************************************/
    [TestFixture]
    public class SimpleQueryTests
    {
        [SetUp]
        public void TestInitialize()
        {
            SampleData.CreateSampleData();
        }

        [TearDown]
        public void TestCleanup()
        {
            SampleData.DeleteSampleData();
        }

        [Test]
        public void SampleDataTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books select a;

            Assert.AreEqual(6, all.ToList().Count());
        }

        /************************************************************************************
        * Example SQL:          select * from mydomain where Title = 'The Right Stuff'	
        * Description:          Retrieves all items where the attribute "Title" equals "The Right Stuff."	
        * Expected result:      1579124585
        ************************************************************************************/
        [Test]
        public void EqualsTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.Title == "The Right Stuff" select a;

            Assert.AreEqual(1, all.ToList().Count());
            Assert.AreEqual("1579124585", all.ToList().First().Id);
        }

        /************************************************************************************
        * Example SQL:          select * from mydomain where Year > '1985'	
        * Description:          Retrieves all items where "Year" is greater than "1985." 
        *                       Although this looks like a numerical comparison, it is lexicographical. 
        *                       Because the calendar won't change to five digits for nearly 8,000 years, "Year" is not zero padded.
        * Expected result:      B000T9886K, B00005JPLW, B000SF3NGK
        ************************************************************************************/
        [Test]
        public void GreaterThanTestString()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.Year.GreaterThan("1985") select a;

            Assert.AreEqual(3, all.ToList().Count());
            //Assert.AreEqual("1579124585", all.ToList().First().Id);
        }

        [Test]
        public void GreaterThanTestNumeric()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.YearInt > 1985 select a;

            Assert.AreEqual(3, all.ToList().Count());
            //Assert.AreEqual("1579124585", all.ToList().First().Id);
        }

        /************************************************************************************
        * Example SQL:          select * from mydomain where Rating like '****%'	
        * Description:          Retrieves all items that have at least a 4 star (****) rating. 
        *                       The prefix comparison is case-sensitive and exact and does not match attributes that only have the "4 star" value, such as item B000T9886K.
        *                       Note: The like operator is similar to starts-with and only supports % at the end of the string.
        * Expected result:      0385333498, 1579124585, 0802131786, B000SF3NGK
        ************************************************************************************/
        [Test, Ignore]
        public void StartsWithTest()
        {
            Assert.Inconclusive("Like with multi-valued attributes is not supported yet...");
        }

        /************************************************************************************
        * Example SQL:          select * from mydomain where Pages < '00320'	
        * Description:          Retrieves all items that have less than 320 pages.        
        *                       This attribute is zero padded in the data set and the select expression, which allows for proper lexicographical comparison between the strings. 
        *                       Items without this attribute are not considered.
        * Expected result:      1579124585, 0802131786
        ************************************************************************************/
        [Test]
        public void LessThanTestNumeric()
        {
            var dbContext = new AwsSampleContext();
            //NOTE: Added > 0 clause to deal with default zero value for integers
            var all = from a in dbContext.Books where a.NumberOfPages < 320 & a.NumberOfPages > 0 select a;

            Assert.AreEqual(2, all.ToList().Count());
            //Assert.AreEqual("1579124585", all.ToList().First().Id);
        }
    }
}
